package com.zs.usc.controller;


import com.zs.usc.common.BusinessException;
import com.zs.usc.common.RestRst;
import com.zs.usc.constant.RelationConstraint;
import com.zs.usc.entity.RelationInfo;
import com.zs.usc.security.Authorization;
import com.zs.usc.service.IRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 关联关系表 前端控制器
 * </p>
 *
 * @author ZS
 * @since 2021-12-11
 */
@RestController
@RequestMapping("/relation")
@Api(tags = "关联关系模块", value = "关联关系模块")
public class RelationController {

    @Autowired
    private IRelationService relationService;


    @PostMapping("/saveUseRoleList")
    @ApiOperation(value = "新增用户角色关联关系")
    @Authorization
    public RestRst<Void> saveUseRoleList(@RequestParam List<RelationInfo> lstInsert) {
        // 补充信息
        if (CollectionUtils.isEmpty(lstInsert))
            throw new BusinessException("用户角色关联关系不能为空！");
        lstInsert.forEach(e -> e.setRelationType(RelationConstraint.USER_ROLE.getRelationType()));

        return RestRst.buildSuccess(relationService.saveBatch(lstInsert));
    }


    @PostMapping("/saveRoleMenuList")
    @ApiOperation(value = "新增角色菜单关联关系")
    @Authorization
    public RestRst<Void> saveRoleMenuList(@RequestParam List<RelationInfo> lstInsert) {
        // 补充信息
        if (CollectionUtils.isEmpty(lstInsert))
            throw new BusinessException("新增角色菜单关联关系不能为空！");
        lstInsert.forEach(e -> e.setRelationType(RelationConstraint.ROLE_MENU.getRelationType()));

        return RestRst.buildSuccess(relationService.saveBatch(lstInsert));
    }
}

